package com.itany.mybatis;

import com.itany.mybatis.dao.HeroMapper;
import com.itany.mybatis.entity.Hero;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * Description: 类的描述
 * Author: liudaolin@itany.com
 * Date: 2021/07/21 09:04
 * Version: 1.0 版本信息
 */
public class Test05 {
    private SqlSessionFactory factory;
    private SqlSession session;
    @Before
    public void before() {
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        factory = builder.build(Test01.class
                .getClassLoader()
                .getResourceAsStream("mybatis-config.xml"));
        session = factory.openSession();
        System.out.println("start");
    }
    @After
    public void after() {
        if(null != session){
            session.close();
        }
        System.out.println("end");
    }

    @Test
    public void f1() {
        HeroMapper mapper = session.getMapper(HeroMapper.class);
//        System.out.println("mapper=" + mapper);
        Hero hero = new Hero();
        hero.setName("q123");
        hero.setTypeId(1);
        mapper.insert(hero);
        session.commit();
    }
    @Test
    public void f2() {
        HeroMapper mapper = session.getMapper(HeroMapper.class);
        Hero hero = new Hero();
        hero.setId(21);
        hero.setName("q1231111");
        mapper.updateById(hero);
        session.commit();
    }
    @Test
    public void f3() {
        HeroMapper mapper = session.getMapper(HeroMapper.class);
        mapper.deleteById(21);
        session.commit();
    }
    @Test
    public void f4() {
        HeroMapper mapper = session.getMapper(HeroMapper.class);
        List<Hero> list = mapper.selectAll1();
        list.forEach(System.out::println);
        session.commit();
    }
    @Test
    public void f5() {
        HeroMapper mapper = session.getMapper(HeroMapper.class);
        List<Hero> list = mapper.selectAll2();
        list.forEach(System.out::println);
        session.commit();
    }
    @Test
    public void f6() {
        HeroMapper mapper = session.getMapper(HeroMapper.class);
        List<Hero> list = mapper.selectAll3();
        list.forEach(System.out::println);
        session.commit();
    }
    @Test
    public void f7() {
        HeroMapper mapper = session.getMapper(HeroMapper.class);
        Hero hero = new Hero();
        hero.setTypeId(1);
        hero.setName("a");
        List<Hero> list = mapper.selectByCondition(hero);
        list.forEach(System.out::println);
        session.commit();
    }
}
